package com.sk.ccflower.server.lib.dbUtils;

import java.sql.Connection;
import java.sql.DriverManager;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.PoolingDriver;

public class ConnectionPoolManager {
	
	private static ConnectionPoolManager instance = null;
	private String connectURI = null;
	private String user = null;
	private String password = null;
	private DataSource m_dataSource = null;
	private static final String POOL_NAME = "CCFLOWER_DB";
	private static final String POOL_URI = "jdbc:apache:commons:dbcp:";
	private PoolingDriver poolDriver = null;
	
	public ConnectionPoolManager( String _connectURI, String _user, String _password ) {
		connectURI = _connectURI;
		user = _user;
		password = _password;
		init();
	}
	
	public static ConnectionPoolManager getInstance( String _connectURI, String _user, String _password ) {
		if ( instance == null )
			instance = new ConnectionPoolManager( _connectURI, _user, _password );
		
		return instance;
	}
	
	private void init() {
		try {
			
			BasicDataSource ds = new BasicDataSource();
	        ds.setDriverClassName( "org.gjt.mm.mysql.Driver" );
	        ds.setUsername( this.user );
	        ds.setPassword( this.password );
	        ds.setUrl( connectURI );
	        ds.setInitialSize( 10 );
	        ds.setMaxActive( -1 );
	        ds.setMaxIdle( -1 );
	        ds.setMaxWait( -1 );
	        ds.setMinIdle( 5 );
	        ds.setValidationQuery( "SELECT COUNT(admin_id) FROM admin" );
	        
	        m_dataSource = ds;
	        
	        /*
			Class.forName( "org.gjt.mm.mysql.Driver" );
			GenericObjectPool connectionPool = new GenericObjectPool();
			ConnectionFactory connectionFactory = new DriverManagerConnectionFactory( this.connectURI, this.user, this.password );
			connectionPool.setMaxActive( 10 );
			connectionPool.setMaxIdle( 5 );
			connectionPool.setMaxWait( 1000*10 );
			PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory( connectionFactory , connectionPool , null , null , false , false );
			poolableConnectionFactory.setValidationQuery( "SELECT COUNT(admin_id) FROM admin" );
			
			Class.forName( "org.apache.commons.dbcp.PoolingDriver" );
			PoolingDriver driver=( PoolingDriver )DriverManager.getDriver( POOL_URI );
			driver.registerPool( POOL_NAME , connectionPool );
			this.poolDriver =( PoolingDriver )DriverManager.getDriver( POOL_URI );
			*/
		} catch ( Exception e ) {
			e.printStackTrace();
		}
	}
	
	public Connection getConnection() throws Exception {
		try {
			if ( m_dataSource != null ) {
				return m_dataSource.getConnection();
				//return DriverManager.getConnection( POOL_URI + POOL_NAME );
			}
		} catch ( Exception e ) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	public void relese( Connection con ) {
		if( null != con ){
			try {
				if ( null != this.poolDriver ) {
					this.poolDriver.getConnectionPool( POOL_NAME ).returnObject( con );
				}
			} catch ( Exception ex ) {
				ex.printStackTrace();
			}
		}
	}
	
	public void shutdown() throws Exception {
		this.poolDriver = ( PoolingDriver )DriverManager.getDriver( POOL_URI );
		this.poolDriver.closePool( POOL_NAME );
	}
	
	public int getNumActive()throws Exception {
		
		BasicDataSource ds = (BasicDataSource) this.m_dataSource;
		return ds.getNumActive();
		
		/*
		try {
			ObjectPool pool = this.poolDriver.getConnectionPool( POOL_NAME );
			return pool.getNumActive();
		} catch (SQLException e) {
			throw e;
		}
		*/
	}
	
	public int getNumIdle() throws Exception {
		
		BasicDataSource ds = (BasicDataSource) this.m_dataSource;
		return ds.getNumIdle();
		
		/*
		try {
			ObjectPool pool = this.poolDriver.getConnectionPool( POOL_NAME );
			return pool.getNumIdle();
		} catch (SQLException e) {
			throw e;
		}
		*/
	}
	
}
